package com.rstrulak.managedBeans.controllers;

/**
 * 
 */

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;

import javax.annotation.PostConstruct;

import org.richfaces.event.FileUploadEvent;
import org.richfaces.model.UploadedFile;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.rstrulak.utils.Constants;
import com.rstrulak.utils.JSFUtils;
import com.rstrulak.utils.NoteFile;

@Component("fileUploadBean")
@Scope("session")
public class FileUploadBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4996089809361729398L;
	private ArrayList<NoteFile> files = new ArrayList<NoteFile>();
	private NoteFile file;
	private Long noteId;
	
	@PostConstruct
	public void init(){
		JSFUtils.setSessionParams();
	}

	public int getSize() {
		if (getFiles().size() > 0) {
			return getFiles().size();
		} else {
			return 0;
		}
	}

	public FileUploadBean() {
	}

	public void paint(OutputStream stream, Object object) throws IOException {
		stream.write(getFiles().get((Integer) object).getData());
	}

	public void listener(FileUploadEvent event) throws Exception {
		UploadedFile item = event.getUploadedFile();
		file = new NoteFile();

		file.setLength(item.getData().length);
		file.setName(item.getName());
		file.setData(item.getData());
		files.add(file);
	}

	public void saveNoteFiles() throws IOException {
		JSFUtils.setSessionParams();
		String _path = Constants.NOTE_ATTACHMENTS_PATH;

		File gtdFile = new File(_path);
		if (!gtdFile.exists()) {
			gtdFile.mkdir();
		}
		for (NoteFile noteFile : files) {
			File fileToSave = new File(_path + noteFile.getName());
			FileOutputStream fos = new FileOutputStream(fileToSave);
			fos.write(noteFile.getData());
			fos.flush();
			fos.close();
		}
	}
	
	public void saveTaskFiles() throws IOException {
		JSFUtils.setSessionParams();
		String _path = Constants.TASK_ATTACHMENTS_PATH;

		File gtdFile = new File(_path);
		if (!gtdFile.exists()) {
			gtdFile.mkdir();
		}
		for (NoteFile noteFile : files) {
			File fileToSave = new File(_path + noteFile.getName());
			FileOutputStream fos = new FileOutputStream(fileToSave);
			fos.write(noteFile.getData());
			fos.flush();
			fos.close();
		}
	}
	public void saveProjectFiles() throws IOException {
		JSFUtils.setSessionParams();
		String _path = Constants.PROJECT_ATTACHMENTS_PATH;

		File gtdFile = new File(_path);
		if (!gtdFile.exists()) {
			gtdFile.mkdir();
		}
		for (NoteFile noteFile : files) {
			File fileToSave = new File(_path + noteFile.getName());
			FileOutputStream fos = new FileOutputStream(fileToSave);
			fos.write(noteFile.getData());
			fos.flush();
			fos.close();
		}
	}

	public String clearUploadData() {
		files.clear();
		return null;
	}

	public NoteFile getFile() {
		return file;
	}

	public void setFile(NoteFile file) {
		this.file = file;
	}

	public Long getNoteId() {
		return noteId;
	}

	public void setNoteId(Long noteId) {
		this.noteId = noteId;
	}

	public long getTimeStamp() {
		return System.currentTimeMillis();
	}

	public ArrayList<NoteFile> getFiles() {
		return files;
	}

	public void setFiles(ArrayList<NoteFile> files) {
		this.files = files;
	}

}
